package com.dwin.dingnuo.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.dwin.dingnuo.R;

/**
 * 自定义UI Dialog
 */
public class DwinDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private TextView dialog_tip, dialog_content;
    private TextView tv_positive, tv_negative;

    private MyDialogListener listener;

    private String tip;
    private String content;
    private String positiveTxt;
    private String negativeTxt;

    public DwinDialog setTip(String tip) {
        this.tip = tip;
        return this;
    }

    public DwinDialog setContent(String content) {
        this.content = content;
        return this;
    }

    public DwinDialog setPositiveTxt(String positiveTxt) {
        this.positiveTxt = positiveTxt;
        return this;
    }

    public DwinDialog setNegativeTxt(String negativeTxt) {
        this.negativeTxt = negativeTxt;
        return this;
    }

    public interface MyDialogListener {
        public void onClick(View view);
    }

    public DwinDialog(Context context) {
        super(context);
        this.context = context;
    }

    public DwinDialog(Context context, int style, MyDialogListener listener) {
        super(context, style);
        this.context = context;
        this.listener = listener;
    }

    //    style引用style样式  dialog_dwin_layout
    public DwinDialog(Context context, int style) {
        super(context, style);
    }

    public void setDialogListener(MyDialogListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_dwin_layout);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);

        dialog_tip = findViewById(R.id.dialog_tip);
        dialog_content = findViewById(R.id.dialog_content);
        tv_positive = (TextView) findViewById(R.id.tv_positive);
        tv_negative = (TextView) findViewById(R.id.tv_negative);
        tv_positive.setOnClickListener(this);
        tv_negative.setOnClickListener(this);

        if (tip != null) {
            dialog_tip.setText(tip);
        }
        if (content != null) {
            dialog_content.setText(content);
        }
        if (positiveTxt != null) {
            tv_positive.setText(positiveTxt);
        }
        if (negativeTxt != null) {
            tv_negative.setText(negativeTxt);
        }
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onClick(v);
        }
    }

}
